/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-03
 * Time: 17:10
 */
public class MyBlockingQueue {
    private String[] data;
    private int head;
    private int tail;
    private int size;
    public static Object locker = new Object();
    public MyBlockingQueue(int capacity){
        if(capacity <= 0){
            throw new IllegalArgumentException("capacity must be positive");
        }
        data = new String[capacity];
    }
    public void put(String elem) throws InterruptedException {
        synchronized (locker){
            while(size == data.length){
                locker.wait();
            }
            data[tail] = elem;
            tail++;
            if(tail >= data.length){
                tail = 0;
            }
            size++;
            locker.notify();    //有元素了, 可以取了
        }
    }
    public String take() throws InterruptedException {
        synchronized (locker){
            while(size == 0){
                locker.wait();
            }
            String ret = data[head];
            head++;
            if(head >= data.length){
                head = 0;
            }
            size--;
            locker.notify();
            return ret;
        }
    }
}
